Imports System.Data

Partial Class WebForms_Trustee
    Inherits WebFormCommon

    Dim strPath As String

    Private Sub SetPageSecurity()
        Dim sec As New Secure
        Dim cfunc As New CIMSfunc

        sec.ParentPage = ParentPage
        sec.SetPageAccess(Session("UserName"))

        If ViewState("mode") = "view" Then
            cfunc.FormDisable(divMain)
        End If

        cfunc = Nothing
        sec = Nothing

    End Sub

    Private Sub InitVars()
        If Not IsNothing(Session("Seq_CD")) Then ViewState.Add("Seq_CD", Session("Seq_CD"))
        If Not IsNothing(Session("CID")) Then ViewState.Add("CID", Session("CID"))
        If Not IsNothing(Session("viewtag")) Then ViewState.Add("viewtag", Session("viewtag"))

        If Not IsNothing(Session("PolicyNo")) Then
            ViewState.Add("PolicyNo", Session("PolicyNo"))
            Policy_td.InnerText = ViewState("PolicyNo")
        End If
        If Not IsNothing(Session("Provider")) Then
            ViewState.Add("Provider", Session("Provider"))
            Provider_td.InnerText = ViewState("Provider")
        End If
        If Not IsNothing(Session("CID")) Then ViewState.Add("CID", Session("CID"))
        If Not IsNothing(Session("CName")) Then
            ViewState.Add("CName", Session("CName"))
            name_td.InnerText = ViewState("CName")
            If name_td.InnerText = "" Then name_td.Visible = False
        End If
        If Not IsNothing(Session("mode")) Then ViewState.Add("mode", Session("mode"))
        ViewState.Add("UserName", Session("UserName"))
        Session.Clear()
        Session.Add("UserName", ViewState("UserName"))

        Call SetPageSecurity()
    End Sub

    Private Sub InitSessions()
        If Not IsNothing(ViewState("Seq_CD")) Then Session.Add("Seq_CD", ViewState("Seq_CD"))
        If Not IsNothing(ViewState("PolicyNo")) Then Session.Add("PolicyNo", ViewState("PolicyNo"))
        If Not IsNothing(ViewState("Provider")) Then Session.Add("Provider", ViewState("Provider"))
        If Not IsNothing(ViewState("CID")) Then Session.Add("CID", ViewState("CID"))
        If Not IsNothing(ViewState("CName")) Then Session.Add("CName", ViewState("CName"))
        If Not IsNothing(ViewState("viewtag")) Then Session.Add("viewtag", ViewState("viewtag"))
        If Not IsNothing(ViewState("mode")) Then Session.Add("mode", ViewState("mode"))

        ViewState.Clear()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Select Case Me.PageControlMode
            Case ControlMode.NewBusiness
                strPath = "New_Business"
            Case ControlMode.ClientServicing
                strPath = "Client_Servicing"
        End Select

        If Not Page.IsPostBack Then
            Dim lookup As New LookUp.FillLookUp

            With lookup
                .FillDropDown(Country_cmb, .eLookUpType.country)
                .FillDropDown(City_cmb, .eLookUpType.country_city, , Country_cmb.SelectedValue.ToString)
                .FillDropDown(Gender_cmb, .eLookUpType.other, "GENDER")
                .FillDropDown(Relationship_cmb, .eLookUpType.other, "RELEATIONSHIP")
                .FillDropDown(Ranking_cmb, .eLookUpType.other, "BENEFICIARY_RANK")
                .FillDropDown(IDType_cmb, .eLookUpType.other, "ID_TYPE")
            End With

            Call InitVars()
            Call LoadTrustee()

            Dim sec As New Secure

            sec.ParentPage = ParentPage

            If IsNothing(ViewState("CID")) Then
                sec.AddControl(Save_btn, Secure.ControlTypes.AddType)
            Else
                sec.AddControl(Save_btn, Secure.ControlTypes.EditType)
            End If

            sec.SetPageAccess(Session("UserName"))
            sec = Nothing
        End If
    End Sub

    Private Sub LoadTrustee()
        If IsNothing(ViewState("CID")) Then Exit Sub

        Dim dao As New DAOMain.POLICY_TRUSTEES_DTL_DAO
        Dim vo As New DAOMain.POLICY_TRUSTEES_DTL_VO
        Dim cFunc As New CIMSfunc
        Dim LookUp As New LookUp.FillLookUp

        vo.POLICY_TRUSTEES_CD = ViewState("CID")
        vo.POLICY_SEQ_CD = ViewState("Seq_CD")

        Dim dt As DataTable = dao.Search(vo)

        If dt.Rows.Count > 0 Then
            With dt.Rows(0)

                Birthday_txt.Text = cFunc.ParseGetDate(.Item("BIRTHDAY"))

                Country_cmb.SelectedValue = .Item("COUNTRY_CD")
                LookUp.FillDropDown(City_cmb, LookUp.eLookUpType.country_city, , Country_cmb.SelectedValue.ToString)
                City_cmb.SelectedValue = .Item("CITY_CD")

                Gender_cmb.SelectedValue = .Item("GENDER")
                Relationship_cmb.SelectedValue = .Item("RELATIONSHIP")
                ID_Num.Text = .Item("ID_NO")
                IDType_cmb.SelectedValue = .Item("ID_TYPE")
                Occupation_txt.Text = .Item("OCCUPATION")
                Add1_txt.Text = .Item("POLICY_TRUSTEES_ADDRESS")
                Add2_txt.Text = .Item("POLICY_TRUSTEES_ADDRESS2")
                FirstName_txt.Text = .Item("POLICY_TRUSTEES_FIRST_NAME")
                LastName_txt.Text = .Item("POLICY_TRUSTEES_LAST_NAME")
                MiddleName_txt.Text = .Item("POLICY_TRUSTEES_MIDDLE_NAME")
                Ranking_cmb.SelectedValue = .Item("RANKING_CD")
                Relationship_cmb.SelectedValue = .Item("RELATIONSHIP")
                Zip_txt.Text = .Item("ZIP_CD")

                T1_txt.Text = cFunc.ParsePhoneNo(.Item("TEL_NO"), 1)
                T2_txt.Text = cFunc.ParsePhoneNo(.Item("TEL_NO"), 2)
                T3_txt.Text = cFunc.ParsePhoneNo(.Item("TEL_NO"), 3)
                T4_txt.Text = cFunc.ParsePhoneNo(.Item("TEL_NO"), 4)

                M1_txt.Text = cFunc.ParsePhoneNo(.Item("MOBILE_NO"), 1)
                M2_txt.Text = cFunc.ParsePhoneNo(.Item("MOBILE_NO"), 2)
                M3_txt.Text = cFunc.ParsePhoneNo(.Item("MOBILE_NO"), 3)

            End With
        End If

        LookUp = Nothing
        cFunc = Nothing
        dao = Nothing
        vo = Nothing
    End Sub

    Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        Dim dao As New DAOMain.POLICY_TRUSTEES_DTL_DAO
        Dim vo As New DAOMain.POLICY_TRUSTEES_DTL_VO
        Dim cFunc As New CIMSfunc
        Dim polyFunc As New PolyFunc.ASPFunc

        With vo
            .POLICY_SEQ_CD = ViewState("Seq_CD")

            .BIRTHDAY = cFunc.ParseSetDate(Birthday_txt.Text)
            .CITY_CD = City_cmb.SelectedValue.ToString
            .COUNTRY_CD = Country_cmb.SelectedValue.ToString
            .GENDER = Gender_cmb.SelectedValue.ToString
            .ID_NO = ID_Num.Text
            .ID_TYPE = IDType_cmb.SelectedValue.ToString
            .OCCUPATION = Occupation_txt.Text
            .POLICY_TRUSTEES_ADDRESS = Add1_txt.Text
            .POLICY_TRUSTEES_ADDRESS2 = Add2_txt.Text
            .POLICY_TRUSTEES_FIRST_NAME = FirstName_txt.Text
            .POLICY_TRUSTEES_LAST_NAME = LastName_txt.Text
            .POLICY_TRUSTEES_MIDDLE_NAME = MiddleName_txt.Text
            .RANKING_CD = Ranking_cmb.SelectedValue.ToString
            .RELATIONSHIP = Relationship_cmb.SelectedValue.ToString
            .ZIP_CD = Zip_txt.Text
            .MOBILE_NO = M1_txt.Text & "-" & M2_txt.Text & "-" & M3_txt.Text
            .TEL_NO = T1_txt.Text & "-" & T2_txt.Text & "-" & T3_txt.Text & "-" & T4_txt.Text

            Select Case IsNothing(ViewState("CID"))
                Case True
                    .POLICY_TRUSTEES_CD = cFunc.GetLastID("policy_trustees_cd", "policy_trustees_dtl")
                    .POLICY_TRUSTEES_CREA_BY = Session("UserName")
                    .POLICY_TRUSTEES_CREA_DT = Now

                    If dao.Create_Record(vo) Then
                        InitSessions()
                        Response.Redirect("~/" & strPath & "/ClientInfo.aspx")
                    Else
                        polyFunc.MessageAlert(ParentPage, "Error Saving Record!")
                    End If
                Case False
                    .POLICY_TRUSTEES_CD = ViewState("CID")
                    .POLICY_TRUSTEES_LAST_UPD_BY = Session("UserName")
                    .POLICY_TRUSTEES_LAST_UPD_DT = Now

                    If dao.Update(vo) Then
                        InitSessions()
                        Response.Redirect("~/" & strPath & "/ClientInfo.aspx")
                    Else
                        polyFunc.MessageAlert(ParentPage, "Error Saving Record!")
                    End If
            End Select
        End With

        polyFunc = Nothing
        cFunc = Nothing
        dao = Nothing
        vo = Nothing
    End Sub

    Protected Sub Cancel_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cancel_btn.Click
        InitSessions()
        Response.Redirect("~/" & strPath & "/ClientInfo.aspx")
    End Sub

    Protected Sub Country_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Country_cmb.SelectedIndexChanged
        Dim lookup As New LookUp.FillLookUp

        lookup.FillDropDown(City_cmb, lookup.eLookUpType.country_city, , Country_cmb.SelectedValue.ToString)
        lookup = Nothing
    End Sub

    Protected Sub lbtnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtnSearch.Click
        ClientSearch1.Visible = True
    End Sub

    Protected Sub ClientSearch_Policy_Selected() Handles ClientSearch1.Policy_Selected
        Dim cFunc As New CIMSfunc
        Dim polyFunc As New PolyFunc.ASPFunc
        Dim LookUp As New LookUp.FillLookUp

        With ClientSearch1
            Birthday_txt.Text = cFunc.ParseGetDate(.dtBday)
            Country_cmb.SelectedValue = .strCountry
            LookUp.FillDropDown(City_cmb, LookUp.eLookUpType.country_city, , Country_cmb.SelectedValue.ToString)

            City_cmb.SelectedValue = .strCity
            Add1_txt.Text = .strAdd1
            Add2_txt.Text = .strAdd2
            FirstName_txt.Text = .strFirstName
            LastName_txt.Text = .strLastName
            MiddleName_txt.Text = .strMiddleName
            Gender_cmb.SelectedValue = .strGender

            .Visible = False
        End With

        LookUp = Nothing
        cFunc = Nothing
        polyFunc = Nothing
    End Sub
End Class
